package com.roin.learn.springsecurity.config.mybatisplus;

import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.OptimisticLockerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.pagination.optimize.JsqlParserCountOptimize;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * mybatis-plus相关配置
 */
@Configuration
@MapperScan("com.roin.learn.springsecurity.mapper")
public class MybatisPlusConfig {

    /**
     * 分布插件
     * @return
     */
    public PaginationInnerInterceptor  paginationInnerInterceptor() {
        PaginationInnerInterceptor  paginationInnerInterceptor = new PaginationInnerInterceptor ();
        // 设置请求的页面大于最大页后操作， true调回到首页，false 继续请求  默认false
//         paginationInnerInterceptor.setOverflow(false);
        // 设置最大单页限制数量，默认 500 条，-1 不受限制
//        paginationInnerInterceptor.setMaxLimit(500l);
        // 开启 count 的 join 优化,只针对部分 left join
        return paginationInnerInterceptor ;
    }


    /**
     * 配置插件
     * 3.4.0版本以后使用MybatisPlusInterceptor封装插件
     */
    @Bean
    public MybatisPlusInterceptor optimisticLockerInnerInterceptor(){
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();

        /**
         * 乐观锁插件
         * 在乐观锁字段上使用注释@Vesion
         */
        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());

        /**
         * 分页插件
         */
        interceptor.addInnerInterceptor(paginationInnerInterceptor());

        return interceptor;
    }
}
